<?php

/**
 * agenda_novedad actions.
 *
 * @package    fisio-ud
 * @subpackage novedad
 * @author     Johan AR <j3valentin@gmail.com>
 */
class novedadActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->SProfesionalNovedads = SProfesionalNovedadQuery::create()->find();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->SProfesionalNovedad = SProfesionalNovedadPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->SProfesionalNovedad);
  }

  public function executeNew(sfWebRequest $request)
  {
    $novedad = new SProfesionalNovedad();
    $novedad->setUsuarioId($this->getUser()->getProfile()->getId());
    $this->form = new SProfesionalNovedadForm($novedad);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $novedad = new SProfesionalNovedad();
    $novedad->setUsuarioId($this->getUser()->getProfile()->getId());
    $this->form = new SProfesionalNovedadForm($novedad);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $SProfesionalNovedad = SProfesionalNovedadQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($SProfesionalNovedad, sprintf('Object SProfesionalNovedad does not exist (%s).', $request->getParameter('id')));
    $this->form = new SProfesionalNovedadForm($SProfesionalNovedad);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $SProfesionalNovedad = SProfesionalNovedadQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($SProfesionalNovedad, sprintf('Object SProfesionalNovedad does not exist (%s).', $request->getParameter('id')));
    $this->form = new SProfesionalNovedadForm($SProfesionalNovedad);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $SProfesionalNovedad = SProfesionalNovedadQuery::create()->findPk($request->getParameter('id'));
    $this->forward404Unless($SProfesionalNovedad, sprintf('Object SProfesionalNovedad does not exist (%s).', $request->getParameter('id')));
    $SProfesionalNovedad->delete();

    $this->redirect('novedad/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $SProfesionalNovedad = $form->save();

//      $this->redirect('novedad/edit?id='.$SProfesionalNovedad->getId());
      $this->redirect('novedad/index');
    }
  }
}